<?
/** CoreSeek server class
*	Date:	2011-10-13
*	Author:	terrywu
*/

class CoreSeek{	// class start

	private $config;

	public function __construct(){
		global $coreSeekConfig;
		$this->config = $coreSeekConfig;
	}


	/** 創建配置源
	* @param	String	$content	mysql source 內容
	* @param	int		$restart	是否重啟服務
	* @return	Array
	*/
	public function createSource($content){
		if(trim($content)!=''){
			if(file_put_contents($this->config['source'], $content, true)){
				chmod($this->config['source'], 0777);
				return $this->response(true);
			}else{
				return $this->response(false, 'write file fail');
			}
		}else{
			return $this->response(false, 'content is empty');
		}
	}


	/** 讀取配置源
	* @return	Array
	*/
	public function getSource(){
		if(file_exists($this->config['source'])){
			$content = file_get_contents($this->config['source']);
			return $this->response(true, '', $content);
		}else{
			return $this->response(false, 'file not exists');
		}
	}


	/** 創建複合分詞及同義詞
	* @return	Array
	*/
	public function createUnigram(){

		if(!file_exists($this->config['unigram_txt'])){
			file_put_contents($this->config['unigram_txt'], '', true);
			chmod($this->config['unigram_txt'], 0777);
		}

		if(!file_exists($this->config['thesaurus_txt'])){
			file_put_contents($this->config['thesaurus_txt'], '', true);
			chmod($this->config['thesaurus_txt'], 0777);
		}

		if(!is_writable($this->config['unigram_txt']) || !is_writable($this->config['thesaurus_txt'])){
			return $this->response(false, 'unigram or thesaurus txt unwriteable');
		}

		$recount = db_query_count($this->config['table'], '', $this->config['db']);
		if($recount>0){

			if(!($handle1 = fopen($this->config['unigram_txt'], 'w')) || !($handle2 = fopen($this->config['thesaurus_txt'], 'w'))){
				return $this->response(false, 'unigram or thesaurus txt open fail');
			}
			
			$pagesize = 500;
			$page = intval(($recount-1)/$pagesize)+1;

			for($i=1; $i<=$page; $i++){
				$unigram = '';
				$thesaurus = '';
				$param = array('pagesize'=>$pagesize, 'page'=>$i);
				$data = db_query_hash($this->config['table'], $param, $this->config['db']);
				foreach($data['result'] as $val){
					$unigram = $unigram . $val['name']."\t1\nx:1\n";
					if($val['thesaurus']!=''){
						$thesaurus = $thesaurus . $val['name']."\n-".$val['thesaurus'].",\n";
					}
				}
				if($unigram!=''){
					fwrite($handle1, $unigram);
				}
				if($thesaurus!=''){
					fwrite($handle2, $thesaurus);
				}
			}

			fclose($handle1);
			fclose($handle2);

			return $this->response(true);

		}else{
			return $this->response(false, 'no record');
		}
	}


	/** coreseek服務控制
	* @param	String	$state
	* @return	String
	*/
	public function handle($state=''){
		if(in_array($state, array('start', 'stop', 'restart', 'status', 'genindex', 'genuni', 'genthesaurus'))){
			$sh = 'sh '.$this->config['sh'].' '.$state;
			$result = shell_exec($sh);
			return $result;
		}else{
			return 'the action name must among the commands [ start | stop | restart | status | genindex | genuni | genthesaurus ].';
		}
	}


	/** 輸出信息
	* @param	boolean	$status
	* @param	String	$info
	* @param	String	$data
	* @return	Array
	*/
	private function response($status, $info='', $data=''){
		return array($status, $info, $data);
	}

}	// class end
?>